<?php
namespace backend\models;

use Yii;
use yii\base\Model;
use backend\models\LogAdmin;
use yii\base\Object;

/**
 * Login form
 */
class LoginForm extends Model
{
    public $username;
    public $password;
    public $rememberMe = false;

    private $_user = false;
    
    protected $user;


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
            
            'confirmationValidate' => [
            'username',
            function ($attribute) {
            	if ($this->user !== null) {
            		if ($this->user->getIsBlocked()) {
            			$this->addError($attribute, Yii::t('user', 'Your account has been blocked'));
            		}
            		if ($this->user->getIsDeleted()) {
            			$this->addError($attribute, Yii::t('user', 'Your account has been deleted'));
            		}
            	}
            }
            ],
        ];
    }
    
    
    public function attributeLabels()
    {
    	return [
    	'username' => Yii::t('app', 'LoginUsername'),
    	'password' => Yii::t('app', 'Password'),
    	'rememberMe' =>Yii::t('app','RememberMe'),

    	];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
          //  $user = $this->getUser();

            if ($this->user === null|| !$this->user->validatePassword($this->password)) {
                $this->addError($attribute, Yii::t('app','用户名或密码错误'));
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     *
     * @return boolean whether the user is logged in successfully
     */
    public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
        } else {
            return false;
        }
    }
    /**
     * 添加登录日志 -suiyue
     */
    public function loglogin(){
    	$logAdmin=new LogAdmin();
    	$logAdmin->admin_name=$this->username;
    	 $logAdmin->login_ip=Yii::$app->request->userIP;
    	$logAdmin->createtime=time();
    	$logAdmin->save();
    }
	public function loginrecord()
	{
		$user = User::findOne(['username'=> $this->username]);
		$user->lastlogin_ip = Yii::$app->request->userIP;
		$user->lastlogin_at = time();
		if ($user->save())
		{
		    Yii::$app->session['user_id']=$user->user_id;
			return true;
		}
		return false;
	} 
    /**
     * Finds user by [[username]]
     *
     * @return User|null
     */
    public function getUser()
    {
        if ($this->_user === false) {
            $this->_user = User::findByUsername($this->username);
        }

        return $this->_user;
    }
    
    public function beforeValidate()
    {
    	if (parent::beforeValidate()) {
    		$this->user = User::findOne(['username' => $this->username]);
    		return true;
    		
    	} else {
    		return false;
    	}
    }
}
